/*
 original version from WordPress
 by Alex King http://www.alexking.org/

 tax fix and conversion to Prototype by Dobrica Pavlinusic <dpavlin@rot13.org>
*/


var edButtons = new Array();
var edLinks = new Array();
var edOpenTags = new Array();
var edCanvas = null;

function edButton(id, display, tagStart, tagEnd, access, open) {
	Logger.debug('edButton '+id+': '+display);
	this.id = id;			// used to name the toolbar button
	this.display = display;		// label on button
	this.tagStart = tagStart; 	// open tag
	this.tagEnd = tagEnd;		// close tag
	this.access = access;		// access key
	this.open = open;		// set to -1 if tag does not need to be closed
}

edButtons[edButtons.length] =
new edButton('ed_div'
,'div'
,'<div>\n'
,'\n</div>\n'
,'d'
);

edButtons[edButtons.length] =
new edButton('ed_span'
,'span'
,'<span>'
,'</span>'
,'s'
);

edButtons[edButtons.length] =
new edButton('ed_strong'
,'str'
,'<strong>'
,'</strong>'
,'b'
);

edButtons[edButtons.length] =
new edButton('ed_em'
,'em'
,'<em>'
,'</em>'
,'i'
);

/*
edButtons[edButtons.length] =
new edButton('ed_pre'
,'code'
,'<code>'
,'</code>'
,'c'
);

edButtons[edButtons.length] =
new edButton('ed_block'
,'b-quote'
,'<blockquote>'
,'</blockquote>'
,'q'
);
*/

edButtons[edButtons.length] =
new edButton('ed_link'
,'link'
,''
,'</a>'
,'a'
); // special case

function edLink() {
	this.display = '';
	this.URL = '';
	this.newWin = 0;
}

edButtons[edButtons.length] =
new edButton('ed_table'
,'table'
,'\n<table>\n'
,'\n</table>\n'
,'t'
);

edButtons[edButtons.length] =
new edButton('ed_tr'
,'tr'
,'<tr>'
,'</tr>'
,'r'
);

edButtons[edButtons.length] =
new edButton('ed_td'
,'td'
,'<td>'
,'</td>'
,'e'
);

function edShowButton(name, button, i) {
	if (button.id == 'ed_link') {
		return('<input type="button" id="' + name + '_' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertLink(\''+name+'\', ' + i + ');" value="' + button.display + '" />');
	}
	else {
		return('<input type="button" id="' + name + '_' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertTag(\''+name+'\', ' + i + ');" value="' + button.display + '"  />');
	}
}

function edAddTag(name, button) {
	if (edButtons[button].tagEnd != '') {
		edOpenTags[name][ edOpenTags[name].length ] = button;
		$( name + '_' + edButtons[button].id ).value = '/' + $( name + '_' + edButtons[button].id ).value;
	}
}

function edRemoveTag(name, button) {
	for (i = 0; i < edOpenTags[name].length; i++) {
		if (edOpenTags[name][i] == button) {
			edOpenTags[name].splice(i, 1);
			$( name + '_' + edButtons[button].id ).value = $( name + '_' + edButtons[button].id ).value.replace('/', '');
		}
	}
}

function edCheckOpenTags(name, button) {
	var tag = 0;
	for (i = 0; i < edOpenTags[name].length; i++) {
		if (edOpenTags[name][i] == button) {
			tag++;
		}
	}
	if (tag > 0) {
		return true; // tag found
	}
	else {
		return false; // tag not found
	}
}

function edCloseAllTags( name ) {
	var count = edOpenTags[name].length;
	Logger.info('edCloseAllTags '+name+' count:'+count);
	for (o = 0; o < count; o++) {
		edInsertTag(name, edOpenTags[name][ edOpenTags[name].length - 1 ]);
	}
}

var edActiveToolbar = {};

function edToolbar( name ) {

	if (edActiveToolbar[name]) return false;

	var toolbar_div = name + '_toolbar';

	Logger.info('edToolbar of '+name+' is '+toolbar_div);

	var div = $( toolbar_div );
	if (! div) {
		Logger.error('div not found');
		return false;
	}

	var toolbar = '';

	for (var i = 0; i < edButtons.length; i++) {
		toolbar += edShowButton(name, edButtons[i], i);
	}
	toolbar += '&nbsp;&nbsp';
	toolbar += '<input type="button" id="ed_close" class="ed_button" onclick="edCloseAllTags(\''+name+'\');" title="Close all open tags" value="Close Tags" />';

	div.innerHTML = toolbar;

	edActiveToolbar[name] = 1;
	edOpenTags[name] = new Array();

	Logger.debug('edActiveToolbar['+name+'] = '+edActiveToolbar[name]);
	return false;
}

// insertion code

function edInsertTag(name, i) {

	myField = $( name );
	if (! myField) {
		Logger.error("can't find "+name);
		return false;
	}

	var pos = myField.scrollTop;
	Logger.debug('edInsertTag '+name+','+i+'; scrollTop='+pos);

	//IE support
	if (document.selection) {
		myField.focus();
		 sel = document.selection.createRange();
		if (sel.text.length > 0) {
			sel.text = edButtons[i].tagStart + sel.text + edButtons[i].tagEnd;
		}
		else {
			if (!edCheckOpenTags(name, i) || edButtons[i].tagEnd == '') {
				sel.text = edButtons[i].tagStart;
				edAddTag(name, i);
			}
			else {
				sel.text = edButtons[i].tagEnd;
				edRemoveTag(name, i);
			}
		}
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var cursorPos = endPos;
		var scrollTop = myField.scrollTop;

		if (startPos != endPos) {
			myField.value = myField.value.substring(0, startPos)
							  + edButtons[i].tagStart
							  + myField.value.substring(startPos, endPos)
							  + edButtons[i].tagEnd
							  + myField.value.substring(endPos, myField.value.length);
			cursorPos += edButtons[i].tagStart.length + edButtons[i].tagEnd.length;
		}
		else {
			if (!edCheckOpenTags(name, i) || edButtons[i].tagEnd == '') {
				myField.value = myField.value.substring(0, startPos)
								  + edButtons[i].tagStart
								  + myField.value.substring(endPos, myField.value.length);
				edAddTag(name, i);
				cursorPos = startPos + edButtons[i].tagStart.length;
			}
			else {
				myField.value = myField.value.substring(0, startPos)
								  + edButtons[i].tagEnd
								  + myField.value.substring(endPos, myField.value.length);
				edRemoveTag(name, i);
				cursorPos = startPos + edButtons[i].tagEnd.length;
			}
		}
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
		myField.scrollTop = scrollTop;
	}
	else {
		if (!edCheckOpenTags(name, i) || edButtons[i].tagEnd == '') {
			myField.value += edButtons[i].tagStart;
			edAddTag(name, i);
		}
		else {
			myField.value += edButtons[i].tagEnd;
			edRemoveTag(name, i);
		}
		myField.focus();
	}

	if (pos && myField.scrollTop != pos) myField.scrollTop = pos;
}

function edInsertContent(myField, myValue) {

	var pos = myField.scrollTop;

	Logger.debug('edInsertContent "'+myValue+'", scrollTop='+pos);
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
						  + myValue
							 + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
	} else {
		myField.value += myValue;
		myField.focus();
	}

	if (pos && myField.scrollTop != pos) myField.scrollTop = pos;
}

function edInsertLink(name, i, defaultValue) {

	myField = $( name );
	if (! myField) {
		Logger.error("can't find "+name);
		return false;
	}

	if (!defaultValue) {
		defaultValue = 'http://';
	}
	if (!edCheckOpenTags(name, i)) {
		var URL = prompt('Enter the URL' ,defaultValue);
		if (URL) {
			edButtons[i].tagStart = '<a href="' + URL + '">';
			edInsertTag(name, i);
		}
	}
	else {
		edInsertTag(name, i);
	}
}

/* additional methods on textarea */

/*
	onkeydown="return edTab(event);"
*/
function edTab(event) {
	var e = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (e == 9) {
		edInsertContent(edCanvas, '\t');
		// this is a fix for gecko-based browsers which force
		// focus change on tab. It *should* be harmless for
		// others, since edCavnas is allready focused
		setTimeout('edCanvas.focus();', 1);
		return false;
	}
	return true;
}

/*
	onfocus="return edFocus('template_content');"
*/
function edFocus( name ) {
	Logger.info('edFocus '+name);
	edCanvas = $( name );
	if (! edCanvas) Logger.error("can't find "+name);
	edToolbar( name );
	return false;
}

/*
	onblur="return edBlur();"
*/
function edBlur() {
	Logger.info('edBlur');
	edCanvas = null;
	return false;
}

/*
	call this function for cleanup of toolbar and other
	temporary data.
*/
function edKill( name ) {
	Logger.info('edKill '+name);
	edActiveToolbar[name] = null;
}
